package com.yuebook.smartrpc.framework.helper;

import com.yuebook.smartrpc.framework.cluster.ClusterStrategy;
import com.yuebook.smartrpc.framework.cluster.ClusterStrategyEngine;
import com.yuebook.smartrpc.framework.serialization.common.SerializerType;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author honglei
 * @date 2018/11/17 下午10:58
 */
public class PropertyConfigHelper {

    private static final String PROPERTY_CLASSPATH = "/start-remoting.properties";
    private static final Properties properties = new Properties();

    //ZK服务地址
    private static String zkService = "";
    //ZK session超时时间
    private static int zkSessionTimeout;
    //ZK connection超时时间
    private static int zkConnectionTimeout;
    //序列化算法类型
    private static SerializerType serializeType;
    //每个服务端提供者的Netty的连接数
    private static int channelConnectSize;
    //负载均衡策略
    private static ClusterStrategy clusterStrategy;

    /**
     * 初始化
     */
    static {
        InputStream is = null;
        try {
            is = PropertyConfigHelper.class.getResourceAsStream(PROPERTY_CLASSPATH);
            if (null == is) {
                throw new IllegalStateException("start-remoting.properties can not found in the classpath.");
            }
            properties.load(is);

            zkService = properties.getProperty("zk_service");
            zkSessionTimeout = Integer.parseInt(properties.getProperty("zk_sessionTimeout", "500"));
            zkConnectionTimeout = Integer.parseInt(properties.getProperty("zk_connectionTimeout", "500"));
            channelConnectSize = Integer.parseInt(properties.getProperty("channel_connect_size", "10"));
            String seriType = properties.getProperty("serialize_type");
            serializeType = SerializerType.queryType(seriType);
            if (serializeType == null) {
                throw new RuntimeException("serializeType is null");
            }
            String clustertype = properties.getProperty("cluster_strategy");
            clusterStrategy = ClusterStrategyEngine.queryType(clustertype);
            if (clusterStrategy == null) {
                throw new RuntimeException("clusterStrategy is null");
            }

        } catch (Throwable t) {
            throw new RuntimeException(t);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String getZkService() {
        return zkService;
    }

    public static int getZkSessionTimeout() {
        return zkSessionTimeout;
    }

    public static int getZkConnectionTimeout() {
        return zkConnectionTimeout;
    }

    public static int getChannelConnectSize() {
        return channelConnectSize;
    }

    public static SerializerType getSerializeType() {
        return serializeType;
    }

    public static ClusterStrategy getClusterStrategy() {
        return clusterStrategy;
    }
}
